package main.java.leetcode.中等;

import org.junit.Test;

public class leetcode189_轮转数组 {

    /**
     * 输入: nums = [1,2,3,4,5,6,7], k = 3
     * 输出: [5,6,7,1,2,3,4]
     * 解释:
     * 向右轮转 1 步: [7,1,2,3,4,5,6]
     * 向右轮转 2 步: [6,7,1,2,3,4,5]
     * 向右轮转 3 步: [5,6,7,1,2,3,4]
     *
     * 思路： 先全部翻转
     *       然后反转0 - k-1的
     *       最后反转k - 最后的
     */
    public void rotate(int[] nums, int k) {
        k = k % nums.length;
        revise(nums,0,nums.length-1);
        revise(nums,0,k-1);
        revise(nums,k,nums.length-1);
    }

    public int[] revise(int[] nums,int start,int end){
        while(start<end){
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start+=1;
            end-=1;
        }
        return nums;
    }

    @Test
    public void test(){
        int[] n = new int[]{1,2,3,4,5};
        rotate(n,3);

    }
}
